package com.xqboss.apps.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xqboss.apps.dto.consumer.ConsumerGiftQueryDto;
import com.xqboss.apps.enums.consumer.ConsumerGiftTypeEnum;
import com.xqboss.apps.enums.market.MarketTypeEnum;
import com.xqboss.apps.enums.nft.NftDispatchSourceEnum;
import com.xqboss.apps.service.consumer.ConsumerTeamGiveRecordService;
import com.xqboss.apps.service.order.NftDispatchRecordService;
import com.xqboss.apps.vo.consumer.ConsumerGiftRecordDetailVo;
import com.xqboss.apps.vo.consumer.ConsumerGiftRecordVo;
import com.xqboss.apps.vo.user.UserNFTItemVo;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageList;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 礼物管理
 */
@Api(tags = {"礼物管理"})
@RestController
@RequestMapping("/apps/consumer/gift")
public class ConsumerGiftController extends BaseController {

    @Autowired
    private ConsumerTeamGiveRecordService consumerTeamGiveRecordService;

    @Autowired
    private NftDispatchRecordService nftDispatchRecordService;


    @ApiOperation("分页列表")
    @GetMapping("/page/list")
    public R<List<ConsumerGiftRecordVo>> pageList(ConsumerGiftQueryDto param) {
        param.setUserId(SecurityUtils.getUserId());
        PageList<ConsumerGiftRecordVo> pageList = consumerTeamGiveRecordService.giftList(param);
        return R.ok(pageList.getList(), pageList.getTotal());
    }

    @ApiOperation("详情")
    @GetMapping("/info")
    public R<ConsumerGiftRecordDetailVo> info(@RequestParam Long id) {
        ConsumerGiftQueryDto param = new ConsumerGiftQueryDto();
        param.setId(id);
        param.setUserId(SecurityUtils.getUserId());
        List<ConsumerGiftRecordVo> list = consumerTeamGiveRecordService.giftList(param).getList();
        if (!list.isEmpty()) {
            ConsumerGiftRecordDetailVo detailVo = BeanUtil.copyProperties(list.get(0), ConsumerGiftRecordDetailVo.class);
            detailVo.setNftList(Collections.emptyList());
            // 如果是已发放，并且是装备或者典藏，则获取典藏发放列表
            if (detailVo.getIsComplete() == 1) {
                if (detailVo.getGiftType() == ConsumerGiftTypeEnum.AIRDROP) {
                    if (detailVo.getType() == MarketTypeEnum.NORMAL || detailVo.getType() == MarketTypeEnum.EQUIPMENT) {
                        List<UserNFTItemVo> nftItemList = nftDispatchRecordService.getRecordByOrderItemId(NftDispatchSourceEnum.AIRDROP, detailVo.getId());
                        detailVo.setNftList(nftItemList);
                    }
                } else {
                    List<UserNFTItemVo> nftItemList;
                    if (detailVo.getGiveType() == 1 && StrUtil.isNotBlank(detailVo.getGiveRemark()) && detailVo.getGiveRemark().contains(",")) {
                        nftItemList = nftDispatchRecordService.getRecordByTransfer(NftDispatchSourceEnum.TRANSFER, detailVo.getGiveRemark(), detailVo.getId());
                    } else {
                        nftItemList = nftDispatchRecordService.getRecordByOrderId(NftDispatchSourceEnum.MARKET, detailVo.getOrderId());
                    }
                    detailVo.setNftList(nftItemList);
                }
            }
            return R.ok(detailVo);
        }
        return R.fail("数据不存在");
    }

}
